package com.copera.cn.kaka;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.view.Window;

import com.copera.cn.kaka.FragmentIndicator.OnIndicateListener;

public class TrackActivity extends FragmentActivity {
	public static Fragment[] mFragments;

	private ApplicationCustomer appc;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.fragment);

		setFragmentIndicator(0);
		appc = (ApplicationCustomer) getApplication();
		appc.setType("track");

		//强制为竖屏 
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
	}

	private void setFragmentIndicator(int whichIsDefault) {
		mFragments = new Fragment[3];
		mFragments[0] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_shop);
		mFragments[1] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_track);

		mFragments[2] = getSupportFragmentManager().findFragmentById(
				R.id.fragment_my);

		getSupportFragmentManager().beginTransaction().hide(mFragments[0])
				.hide(mFragments[1]).hide(mFragments[2])
				.show(mFragments[whichIsDefault]).commit();

		FragmentIndicator mIndicator = (FragmentIndicator) findViewById(R.id.indicator);
		FragmentIndicator.setIndicator(whichIsDefault);
		mIndicator.setOnIndicateListener(new OnIndicateListener() {
			@Override
			public void onIndicate(View v, int which) {
				
				if(which==1&&appc.getType()!=null&&appc.getType().equals("rule")){
					appc.setType("track");
					MyFragment mf= new MyFragment();
					getSupportFragmentManager().beginTransaction().replace(R.id.fragment_my, mf).commit();
					//getSupportFragmentManager().beginTransaction().replace(R.id.fragment_track, mf).commit();
					TrackRecordFragment tf = new TrackRecordFragment();
					getSupportFragmentManager().beginTransaction().replace(R.id.fragment_track, tf).commit();
					
				}else if(which==2){
					appc.setType("rule");
					TrackRecordFragment tf = new TrackRecordFragment();
					getSupportFragmentManager().beginTransaction().replace(R.id.fragment_my, tf).commit();
					MyFragment mf= new MyFragment();
					getSupportFragmentManager().beginTransaction().replace(R.id.fragment_track, mf).commit();
					
				}
				mFragments[2].setUserVisibleHint(false);
				getSupportFragmentManager().beginTransaction()
						.hide(mFragments[0]).hide(mFragments[1])
						.hide(mFragments[2]).show(mFragments[which]).commit();

			}
		});
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
